#pragma once
#include<assert.h>
#include<iostream>
#include<string>

using namespace std;
namespace Solutoin
{

	template<class T>
	class vector
	{
	public:
		typedef T* iterator;

		vector(size_t n)
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
			
		{
			reserve(n);
			if (_start)
				resize(n, 0);
		}
		vector()
		{}
		vector(size_t n, const T& val = T())
		{
			resize(n, val);
		}

		vector(const vector<T>&v)
		{
			/*vector tmp;
			tmp = v;
			swap(v);*/
			reserve(size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			reserve(last - first);
			while (first!=last)
			{
				push_back(*first);
				first++;
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[]_start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		//vector<T>& operator=(vector<T>v)
		//{
		//	swap(v);
		//	return *this;
		//}

		void clear()
		{
			_finish = _start;
		}
		

		vector<T>& operator=(const vector<T>&v)
		{
			clear();
			if (this != &v)
			{
				reserve(v.capacity());
				for (auto it : v)
				{
					push_back(it);
				}
			}

			return *this;
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}
		const iterator begin()const
		{
			return _start;
		}

		const iterator end()const
		{
			return _finish;
		}

		bool empty()
		{
			return _start == _finish;
		}

		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			*_finish++ = val;
		}

		void pop_back()
		{
			assert(size() != 0);
			_finish--;
		}

		iterator insert(iterator pos,T val)
		{
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			} 
			iterator end = _finish++;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = val;
			return pos;
		}
		iterator erase(iterator position);
		iterator erase(iterator first, iterator last);

		iterator reserve(size_t n);

		void resize(size_t n,T val = T())
		{
			reserve(n);
			if (n < size())
			{
				_finish = n + _start;
			}
			else
			{
				while (_finish<n+_start)
				{
					*_finish++ = val;
				}
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(v._end_of_storage, _end_of_storage);
			std::swap(v._finish, _finish);
			std::swap(v._start, _start);
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return *(_start + pos);
		}
		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return *(_finish + pos);
		}

		size_t capacity()const
		{
			return _end_of_storage - _start;
		}

		size_t size()const
		{
			return _finish - _start;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	template<class T>
	typename vector<T>::iterator vector<T>::reserve(size_t n)
	{
		if (capacity() < n)
		{
			size_t old_size = size();
			iterator tmp = new T[n];
			for (size_t i = 0; i < old_size; i++)
			{
				tmp[i] = _start[i];
			}
			delete[]_start;
			_start = tmp;
			_finish = tmp + old_size;
			_end_of_storage = tmp + n;
		}
		return _start;
	}
	template<class T>
	typename vector<T>::iterator vector<T>::erase(iterator position)
	{
		assert(!empty());
		iterator begin = position;
		while (begin != _finish - 1)
		{
			*begin = *(begin + 1);
			begin++;
		}
		_finish--;
		return position;
	}
	template<class T>
	typename vector<T>::iterator vector<T>::erase(iterator first, iterator last)
	{
		assert(_start <= first && last < _finish);
		size_t len = last - first;
		while (first + len< _finish)
		{
			*first = *(first + len);
			first++;
		}
		_finish = first;
		return _finish;
	}


	template<class Container>
	void print_vector(const Container& v)
	{
		auto it = v.begin();
		while (it != v.end())
		{
			cout << *it++ << ' ';
		}
		cout << endl;
	}

	void testvector1()
	{
		vector<string>v(12, "hello world!");
		vector<string>v1 = v;
		print_vector(v1);
		/*
		vector<int>v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << ' ';
		}
		cout << endl;
		for (auto vh : v)
		{
			cout << vh << ' ';
		}
		cout<<endl;
		v1.pop_back();
		v1.pop_back();
		v1.pop_back();
		//v1.pop_back();
		//v1.pop_back();*/
		//vector<int>v1(6);
		//v1.push_back(1);
		//v1.push_back(2);
		//v1.push_back(5);
		//v1.push_back(5);
		//v1.push_back(5);
		//v1.push_back(5);
		//v1.push_back(3);
		//v1.push_back(4);
		//vector<int>v2(v1.begin(), v1.end());
		////int x;
		////cin >> x;
		////auto p = find(v1.begin(), v1.end(), x);
		////if (p != v1.end())
		////{
		////	p = v1.insert(p, 40);
		////	//*(p) *= 10;
		////}
		////v1.erase(find(v1.begin(), v1.end(), 5), find(v1.begin(), v1.end(), 3));
		////v1.resize(10, 1);
		//print_vector(v2);

	}
}